package yxy.game.pm2.command.camptower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.camptower.CampTowerHandle;
import yxy.game.pm2.module.camptower.CampTowerModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 阵营塔单个UI
 *
 * @ClassName CMD_CampTowerSingleUI
 * @Author jingtao-zheng
 * @Date 2022/8/1 15:58
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerSingleUI, classtype = CampTowerMessage.CampTowerSingleUIRequest.class)
public class CMD_CampTowerSingleUI extends Command<CampTowerMessage.CampTowerSingleUIRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerSingleUIRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        CampTowerMessage.CampTowerSingleUIResponse.Builder response = CampTowerMessage.CampTowerSingleUIResponse.newBuilder();
        //获取阵营塔类型
        int type = request.getType().getNumber();
        //回传type
        response.setType(request.getType());
        //玩家选择的章节,第一次选择的为空
        int chooseChapter = request.getChapter();
        //判断阵营塔是否开启
        boolean finish = CampTowerHandle.isFinish(type);
        if (!finish) {
            send(messageSource, response, Result.ActNoOpen);
        }
        CampTowerModule campTowerModule = CampTowerModule.get(playerCode);
        //获取挑战次数
        Map<Integer, Integer> alltimes = campTowerModule.getTimes();
        Integer times = alltimes.get(type);
        response.setTimes(times);
        //获取章节数
        Map<Integer, Integer> allchapter = campTowerModule.getChapter();
        Integer chapter = allchapter.get(type);
        List<Integer> starOfTwoChapter = campTowerModule.getStarOfTwoChapter().get(type);
        //获取最新层数
        Map<Integer, Integer> allfloor = campTowerModule.getFloor();
        Integer highFloor = allfloor.get(type);
        //当前打过的最高层数
        response.setMaxfloor(highFloor);
        if (chooseChapter == 0) {
            response.setIsFirst(1);
        } else {
            response.setIsFirst(2);
        }
        //第一次进来,或者选择的章节为最新章节
        if (chooseChapter == 0 || chooseChapter == chapter) {
            //如果章节为0-说明玩家刚进入单个阵营塔界面-返回最近打的章节和层数
            response.setChapter(chapter);
            Integer floor = allfloor.get(type);
            response.setFloor(floor);
            //获取最新的层数和星星
            if (Constant.RECORD_FROOL > chapter) {
                //前x章特殊处理
                //获取下标
                int indexStart = (chapter - 1) * Constant.CHAPTER_OF_FLOOR;
                int indexEnd = indexStart + (floor - 1) % Constant.CHAPTER_OF_FLOOR;
                for (int i = indexStart; i <= indexEnd; i++) {
                    CampTowerMessage.FloorAndStar.Builder floorAndStar = CampTowerMessage.FloorAndStar.newBuilder();
                    floorAndStar.setFloor(i + 1);
                    Integer starNum = starOfTwoChapter.get(i);
                    floorAndStar.setStar(starNum);
                    response.addFloorAndStar(floorAndStar);
                }
            } else {
                //非前x章的最新章节获取最后一章的星星
                //计算最后一章最小下标
                int indexStart = (Constant.RECORD_FROOL - 1) * Constant.CHAPTER_OF_FLOOR;
                int indexEnd = indexStart + (floor - 1) % Constant.CHAPTER_OF_FLOOR;
                int minFloor = (chapter - 1) * Constant.CHAPTER_OF_FLOOR + 1;
                for (int i = indexStart, j = 0; i <= indexEnd; i++, j++) {
                    CampTowerMessage.FloorAndStar.Builder floorAndStar = CampTowerMessage.FloorAndStar.newBuilder();
                    floorAndStar.setFloor(minFloor + j);
                    Integer starNum = starOfTwoChapter.get(i);
                    floorAndStar.setStar(starNum);
                    response.addFloorAndStar(floorAndStar);
                }
            }
        } else if (chooseChapter < chapter) {
            //如果章节不为0,则玩家自己选的章节且不是最新章节
            response.setChapter(chooseChapter);
            //返回当前章节的最大层数
            int maxFloor = chooseChapter * Constant.CHAPTER_OF_FLOOR;
            response.setFloor(maxFloor);
            if (chapter - chooseChapter >= Constant.RECORD_FROOL) {
                //已删除的章节,全部返回满心
                //计算选择章节最低层数
                int minFloor = (chooseChapter - 1) * Constant.CHAPTER_OF_FLOOR + 1;
                for (int i = 0; i < Constant.CHAPTER_OF_FLOOR; i++) {
                    CampTowerMessage.FloorAndStar.Builder floorAndStar = CampTowerMessage.FloorAndStar.newBuilder();
                    floorAndStar.setFloor(i + minFloor);
                    floorAndStar.setStar(3);
                    response.addFloorAndStar(floorAndStar);
                }
            } else {
                //未删除的章节
                //该章节最小层数
                int minFloor = ((chooseChapter - 1) * Constant.CHAPTER_OF_FLOOR + 1);
                //计算下标
                int index = (Constant.RECORD_FROOL - (chapter - chooseChapter + 1)) * Constant.CHAPTER_OF_FLOOR;
                //前X章特殊处理
                if (chapter < Constant.RECORD_FROOL) {
                    index = (chooseChapter - 1) * Constant.CHAPTER_OF_FLOOR;
                }
                for (int i = 0; i < Constant.CHAPTER_OF_FLOOR; i++) {
                    CampTowerMessage.FloorAndStar.Builder floorAndStar = CampTowerMessage.FloorAndStar.newBuilder();
                    floorAndStar.setFloor(i + minFloor);
                    Integer starNum = starOfTwoChapter.get(i + index);
                    floorAndStar.setStar(starNum);
                    response.addFloorAndStar(floorAndStar);
                }
            }
        } else if (chooseChapter == chapter + 1) {
            //大于当前章节处理 全部为零星星 返回最小层数
            response.setChapter(chooseChapter);
            //返回当前章节的最小层数
            int minFloor = (chooseChapter - 1) * Constant.CHAPTER_OF_FLOOR + 1;
            response.setFloor(minFloor);

            for (int i = 0; i < Constant.CHAPTER_OF_FLOOR; i++) {
                CampTowerMessage.FloorAndStar.Builder floorAndStar = CampTowerMessage.FloorAndStar.newBuilder();
                floorAndStar.setFloor(i + minFloor);
                floorAndStar.setStar(0);
                response.addFloorAndStar(floorAndStar);
            }

        }
        //获取保存的章节
        ArrayList<Integer> reChapter = new ArrayList<>();
        int minChapter = 0;
        //前x章特殊处理
        if (chapter <= Constant.RECORD_FROOL) {
            for (int i = 1; i <= chapter; i++) {
                reChapter.add(i);
            }
            minChapter = 1;
        } else {
            for (int i = chapter - Constant.RECORD_FROOL + 1; i <= chapter; i++) {
                reChapter.add(i);
            }
            minChapter = chapter - Constant.RECORD_FROOL + 1;
        }

        //获取领取的星星奖励的id,每章是12个奖励
        Map<Integer, List<Integer>> starReward = campTowerModule.getStarReward();
        List<Integer> starIds = starReward.get(type);

        for (int i = 1; i <= Constant.ALL_CHAPTER; i++) {
            //没打的章节
//            if (i > chapter) {
//                chapterAndStar.setChapternum(i);
//                chapterAndStar.setStarnum(0);
//                chapterAndStar.setCanget(1);
//                response.addChapterAndStar(chapterAndStar);
//                continue;
//            }
            //已删除的章节
            if (i < minChapter) {
                CampTowerMessage.ChapterAndStar.Builder chapterAndStar = CampTowerMessage.ChapterAndStar.newBuilder();
                chapterAndStar.setChapternum(i);
                chapterAndStar.setStarnum(Constant.CHAPTER_OF_FLOOR * Constant.RECORD_FROOL);
                //计算是否领完奖励
                chapterAndStar.setCanget(1);
                for (int j = (i - 1) * Constant.STAR_JL_SUM + 1; j <= i * Constant.STAR_JL_SUM; j++) {
                    if (!starIds.contains(j)) {
                        chapterAndStar.setCanget(2);
                    }
                }
                response.addChapterAndStar(chapterAndStar);
                continue;
            }
        }

        //保存的章节
        for (int i = minChapter; i <= chapter; i++) {
            CampTowerMessage.ChapterAndStar.Builder chapterAndStar = CampTowerMessage.ChapterAndStar.newBuilder();
            chapterAndStar.setChapternum(i);
            //计算星星
            int starnum = 0;
            for (int j = Constant.CHAPTER_OF_FLOOR * (i - minChapter); j < Constant.CHAPTER_OF_FLOOR * (i - minChapter + 1) && j < starOfTwoChapter.size(); j++) {
                starnum += starOfTwoChapter.get(j);
            }
            chapterAndStar.setStarnum(starnum);
            //计算是否领完奖励
            chapterAndStar.setCanget(1);
            for (int j = (i - 1) * Constant.STAR_JL_SUM + 1; j <= i * Constant.STAR_JL_SUM; j++) {
                if (!starIds.contains(j)) {
                    chapterAndStar.setCanget(2);
                }
            }
            response.addChapterAndStar(chapterAndStar);
        }
        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerSingleUIResponse.Builder response, Result
            result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
